import gtk
import sys
import os, shutil, stat
import time, datetime

class Main:
	
	def __init__(self, widgetInstance, pathInstance, mimeModule):
		self.__view = widgetInstance
		self.engine = Engine(mimeModule)
		self.path = Path(pathInstance, self)
    
	def setUp(self):
    	
		def makeModel(visible = True):
			self.__model = gtk.ListStore(str, str, str, str, str)
			self.__view.set_model(self.__model)
			self.__view.set_headers_visible(visible)
        	
		def makeFiletypeColumn(title, resizable, visible = 1):
			renderer = gtk.CellRendererText()
			self.__type_column = gtk.TreeViewColumn(title, renderer, text = 0)
			self.__type_column.set_visible(visible)
			self.__view.append_column(self.__type_column)
        
		def makeIconsColumn(title, resizable, visible = 1):
			renderer = gtk.CellRendererPixbuf()
			renderer.set_property("stock-size", 24)
			self.__image_column = gtk.TreeViewColumn(title, renderer, icon_name = 1)
			self.__image_column.set_resizable(resizable)
			self.__image_column.set_visible(visible)
			self.__image_column.set_sort_column_id(0)
			self.__view.append_column(self.__image_column)
        
		def makeFilenameColumn(title, resizable, visible = 1):
			renderer = gtk.CellRendererText()
			self.__title_column = gtk.TreeViewColumn(title, renderer, text = 2)
			self.__title_column.set_resizable(resizable)
			self.__title_column.set_visible(visible)
			self.__title_column.set_sort_column_id(1)
			self.__view.append_column(self.__title_column)
        
		def makeSizeColumn(title, resizable, visible = 1):
			renderer = gtk.CellRendererText()
			self.__size_column = gtk.TreeViewColumn(title, renderer, text = 3)
			self.__size_column.set_resizable(resizable)
			self.__size_column.set_visible(visible)
			self.__size_column.set_sort_column_id(2)
			self.__view.append_column(self.__size_column)
        
		def makeLasteditColumn(title, resizable, visible = 1):
			renderer = gtk.CellRendererText()
			self.__mtime_column = gtk.TreeViewColumn(title, renderer, text = 4)
			self.__mtime_column.set_resizable(resizable)
			self.__mtime_column.set_visible(visible)
			self.__mtime_column.set_sort_column_id(3)
			self.__view.append_column(self.__mtime_column)
			
		makeModel()
		makeFiletypeColumn("-", False, 0)
		makeIconsColumn("#", False)
		makeFilenameColumn("Filename", True)
		makeSizeColumn("Size", True)
		makeLasteditColumn("Last edit", True)
		self.populate()
    
	def populate(self):
		self.__shutdown()
		columns = self.engine.listFiles()
		try:
			for row in columns:
				self.__model.append([ row[0], row[1], row[2], row[3], row[4] ])
		except:
			pass
    
	def __shutdown(self): self.__model.clear()
	
	def onClick(self, *args):
		treeselection = self.__view.get_selection()
		(tmp_model, tmp_iter) = treeselection.get_selected()
		type = tmp_model.get_value(tmp_iter, 0)
		name = tmp_model.get_value(tmp_iter, 2)
		if type == "directory":
			self.engine.cwd(os.path.join(self.engine.pwd(), name))
			self.populate()
			self.path.update(self.engine.pwd())
		else:
			try:
				open = os.system("xdg-open '" + name +"'")
			except Exeption, e:
				print e
		# What about printing on LogView something? :-|
		#import absynth
		#tryd = absynth.Absynth("")
		#tryd.logViewWidget.write("Something?")

class Engine:

	def __init__(self, mimeModule):
		if os.path.isfile('/usr/bin/gnome-session'):
			self.__mime = mimeModule.Gnome()
		else:
			self.__mime = mimeModule.notGnome()
	
	def pwd(self):
		return os.getcwd()
	
	def cwd(self, name):
		os.chdir(name)
		
	def delete(self, name):
		os.remove(name)
		
	def rmdir(self, name):
		shutil.rmtree(name)
		
	def listFiles(self, defaultpath = ".", showhidden = 0):
		try:
			mylist, listdata = os.listdir(defaultpath), [("directory", self.__mime.return_folder_mime(), ".." , "4096", "")]
		except:
			return None
    
		for myitem in mylist:
			if showhidden == 0 and myitem[0] == ".": continue
			mystats = os.stat(os.path.join(".", myitem))
			(txt_type, type, name, size, lastedit) = \
				(os.path.isdir(myitem) and "directory" or "file", 
				os.path.isdir(myitem) and self.__mime.return_folder_mime() or self.__mime.return_mime(myitem), 
				myitem, 
				str(mystats.st_size), 
				str(datetime.datetime.fromtimestamp(mystats.st_mtime)))
			listdata.append((txt_type, type, name, size, lastedit))
			
		listdata.sort()
		return listdata
	
class Path:

	def __init__(self, widgetInstance, TreeInstance):
		self.__path = widgetInstance
		self.update(os.getcwd())
		self.__widget = TreeInstance
		
	def update(self, path):
		self.__path.set_text(path)
		
	def onChange(self, *args):
		self.__widget.engine.cwd(self.__path.get_text())
		self.__widget.populate()